package hugo

import (
	"gitee.com/lzqysoft/ims/libs"
	"github.com/astaxie/beego"
	"os"
	"time"
)

func fmtArray(strArr []string) string {
	str := ""
	for i := 0; i < len(strArr); i++ {
		if i == 0 {
			str += `["` + strArr[i] + `"]`
		} else if i == len(strArr)-1 {
			str += `"` + strArr[i] + `"]`
		} else {
			str += `"` + strArr[i] + `",`
		}
	}
	return str
}

func getMdInfo(name, author, banner string, tags []string) string {
	str := `+++
title = "` + name + `"
date= "` + time.Now().String() + `"
tags = ` + fmtArray(tags) + `
author = "` + author + `"
banner = "` + banner + `"
+++
`
	return str
}

func Writemd(name, author, banner, path, content string, tags []string) (locate string, err error) {

	newName := libs.UUID() + ".md"
	f, err := os.Create(path + newName)
	if err != nil {
		beego.Debug(err)
		return
	}
	defer f.Close()
	info := getMdInfo(name, author, banner, tags)
	_, err = f.WriteString(info)
	if err != nil {
		beego.Debug(err)
		return
	}
	_, err = f.WriteString(content)
	if err != nil {
		beego.Debug(err)
		return
	}
	locate = path + newName
	beego.Debug("create new file: ", locate)
	return locate, nil
}
